import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const WHITE_LIST = ['/404', '/login']

router.beforeEach((to, from, next) => {
  NProgress.start()
  const token = store.getters.token
  if (token) {
    // 存在
    if (to.path === '/login') {
      next('/')
      NProgress.done()
    } else {
      next()
    }
  } else {
    // 没有token
    if (WHITE_LIST.indexOf(to.path) !== -1) {
      next()
    } else {
      // next(`/login?_redirect=${to.path}`)
      router.push({ path: '/login', query: { _redirect: to.path }})
      // router.push({ name: 'Login', params: { _redirect: to.path }})
    }
  }
})
router.afterEach((to, from) => {
  NProgress.done()
})
